<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改【精彩瞬间】')" />
    <th:block th:include="include :: header('文件上传')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-img-edit" th:object="${tbWebsiteImg}">
        <input name="id" th:field="*{id}" id="id" type="hidden">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>门户网站图片上传</h5>
                </div>
                <div class="ibox-title">
                    <h5>先前上传的图片</h5>
<!--
                    <p><img th:src="@{{src}(src=${tbWebsiteImg.src})}" style="width: 660px;height: 300px"/></p>
-->
                    <p><img th:src="${tbWebsiteImg.src}" style="width: 660px;height: 300px"/></p>
                </div>
                <!--前端如何显示回显图片？？-->
                <div class="ibox-content">
                    <div class="form-group">
                        <label class="font-noraml">单图片文件上传</label>
                        <div class="file-loading">
                            <input id="file" type="file" multiple name="file">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>

</div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
        //发送图片id及其新上传图片
        var prefix = ctx + "website/img";
        $("#form-img-edit").validate({
            focusCleanup: true
        });

        //修改提交
        function submitHandler() {
            if ($.validate.form()) {
                var formData = new FormData();
                formData.append("file", $("#file")[0].files[0]);
                formData.append("id", $("#id").val());
                console.log($("#file")[0].files[0])
                if ($("#file")[0].files[0] == null) {
                    $.modal.msgError("请选择图片上传！")
                } else {
                    var name=$("#file")[0].files[0]['name'];
                    var suffix=name.substring(name.lastIndexOf('.'),name.length);
                    if(suffix == '.jpg' || suffix=='.png' || suffix=='.gif') {
                        $.ajax({
                            url: prefix + '/upload',
                            type: 'post',
                            data: formData,
                            contentType: false,
                            processData: false,
                            dataType: "json",
                            success: function (data) {
                                if (data.code == 0) {
                                    $.modal.msgReload(data.msg, 'success');
                                } else if(data.code==301){
                                    $.modal.msg(data.msg,'warn');
                                } else {
                                    $.modal.msgReload(data.msg, 'error');
                                }
                            }
                        })
                    }else{
                        $.modal.msgError("请选择图片上传！")
                    }
                }
            }
        }

        $(document).ready(function () {
            //获得额外参数的方法
            fodderType = function() {
                return $("#id").val();
            };

            //初始化文件上传
            $("#file").fileinput({
                'theme': 'explorer-fas',
                'uploadUrl': '/website/img/upload',
                overwriteInitial: false,
                initialPreviewAsData: true,
                contentType: false,
                allowedFileExtensions : ['jpg', 'png','gif'],
                showUpload: false,
                uploadExtraData: function(previewId, index) {   //额外参数的关键点
                    var obj = {};
                    obj.id = fodderType();
                    console.log(obj);
                    return obj;
                }
            });
            // 上传附件
            // 上传成功回调
            $("#file").on("filebatchuploadcomplete", function() {
                $.modal.msgReload("上传附件成功",'success');
                //setTimeout("closeUpladLayer()",2000)
            });
            // 上传失败回调
            $('#file').on('fileerror', function(event, data, msg) {
                $.modal.msgReload("上传附件失敗", 'error');
                //tokenTimeOut(data);
            });
        });
    </script>
</body>
</html>